package com.carefree.bookmall.service.impl;

import com.alibaba.fastjson.JSON;
import com.carefree.bookmall.common.utils.Constant;
import com.carefree.bookmall.common.vo.BookQueryVo;
import com.carefree.bookmall.common.vo.PageVO;
import com.carefree.bookmall.dao.BookMapper;
import com.carefree.bookmall.dao.BookTypeMapper;
import com.carefree.bookmall.entity.BookType;
import com.carefree.bookmall.exception.CustomException;
import com.carefree.bookmall.service.BookTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName:BookTypeServiceImpl
 * @Description:
 * @Author Tengbin
 */
@Service(value = "bookTypeService")
public class BookTypeServiceImpl implements BookTypeService {

    @Resource
    private BookMapper bookMapper;

    @Resource
    private BookTypeMapper bookTypeMapper;


    @Resource
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 查询书籍类别
     *
     * @return List<BookType>
     */
    @Override
    public List<BookType> queryBookTypes() {
        String bookTypesStr=  redisTemplate.opsForValue().get(Constant.REDIS_BOOKTYPES);
        if (StringUtils.isBlank(bookTypesStr)) {
            List<BookType> bookTypes = bookTypeMapper.selectBookTypes();
            redisTemplate.opsForValue().set(Constant.REDIS_BOOKTYPES, JSON.toJSONString(bookTypes),30, TimeUnit.MINUTES);
            return bookTypes;
        }
        return (List<BookType>) JSON.parse(bookTypesStr);

    }


    /**
     * 分页查询书籍类别
     *
     * @param pageNum  页码
     * @param pageSize 每页显示记录数
     * @return
     */
    @Override
    public PageVO<BookType> queryBookTypes(int pageNum, int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        // 分页查询所有类别信息
        List<BookType> bookTypes = bookTypeMapper.selectBookTypesByPage((pageNum - 1) * pageSize, pageSize);
        // 实例化自定义分页对象并赋值
        PageVO<BookType> pageVO = new PageVO<>();
        pageVO.setPageNumber(pageNum);
        pageVO.SetPageSizeAndTotalCount(pageSize,  bookTypeMapper.selectBookTypeCount());
        pageVO.setList(bookTypes);

        return pageVO;
    }

    /**
     * 查询所有分类的书籍信息
     *
     * @param pageNum  页码
     * @param pageSize 每页显示记录数
     * @return
     */
    @Override
    public PageVO queryAllBooksForTypes(int pageNum, int pageSize) {
        if (pageNum <= 0) {
            pageNum = 1;
        }
        // 查询书籍总数
        int totalCount = bookMapper.selectCountOfBooks();
        // 分页查询书籍
        List<BookQueryVo> books = bookMapper.selectBooks((pageNum - 1) * pageSize, pageSize);

        // 实例化自定义分页对象并赋值
        PageVO pageVO = new PageVO();
        pageVO.setPageNumber(pageNum);
        pageVO.SetPageSizeAndTotalCount(pageSize, totalCount);

        pageVO.setList((List) books);

        return pageVO;
    }

    /**
     * 根据书籍类别id分页查询书籍信息
     *
     * @param typeId     书籍类别id
     * @param pageNumber 页数
     * @return page
     */
    @Override
    public PageVO queryBookByBooktypeId(Integer typeId, int pageNumber, int pageSize) {

        if (pageNumber <= 0) {
            pageNumber = 1;
        }
        // 查询指定类别id总数
        int totalCount = bookTypeMapper.selectCountOfBooksByTypeId(typeId);

        // 根据类别id查询对应的书籍信息
        List<BookQueryVo> books = bookTypeMapper.selectBooksByTypeId(typeId, (pageNumber - 1) * pageSize, pageSize);

        // 实例化自定义page对象并赋值最后返回
        PageVO pageVO = new PageVO();
        pageVO.setPageNumber(pageNumber);
        pageVO.SetPageSizeAndTotalCount(pageSize, totalCount);
        pageVO.setList((List) books);

        return pageVO;
    }

    /**
     * 根据书籍类别id查询书籍类别
     *
     * @param typeId 书籍类别id
     * @return 类别名称
     */
    @Override
    public String queryBookTypeNameByBookTypeId(Integer typeId) {
        return bookTypeMapper.selectBookTypeNameByTypeId(typeId);
    }

    /**
     * 添加书籍类别
     *
     * @param typeName 类别名称
     * @return true or false
     */
    @Override
    public boolean addBookType(BookType bookType) {
        if (bookType.getStatus() == null) {
            bookType.setStatus(1);
        }
        return bookTypeMapper.addBookType(bookType) > 0;
    }

    /**
     * 删除书籍类别信息
     *
     * @param typeId 书籍类别id
     * @return true or false
     */
    @Override
    public boolean removeBookType(Integer typeId) throws CustomException {
        if (typeId == null) {
            throw new CustomException("类目的主键id必须不为空！");
        }
        return bookTypeMapper.deleteBookType(typeId) > 0;
    }

    /**
     * 修改书籍类别下信息
     *
     * @param typeId   书籍类别id
     * @param typeName 修改后的书籍类别名称
     * @return true or false
     */
    @Override
    public boolean modifyBookType(BookType bookType) {
        if (bookType.getStatus() == null) {
            bookType.setStatus(1);
        }
        return bookTypeMapper.updateBookType(bookType) > 0;
    }

    /**
     * 根据编号查询
     *
     * @param typeId 主键id
     * @return
     */
    @Override
    public BookType queryBookTypeById(Integer typeId) throws CustomException {
        if (typeId == null) {
            throw new CustomException("类目id必须不为空！");
        }

        return bookTypeMapper.selectBookTypeById(typeId);
    }

    /**
     * 根据分类对应的访问路径查询对应名称
     *
     * @param typeUrl
     * @return
     */
    @Override
    public BookType queryBookTypeUrl(String typeUrl) throws CustomException {
        BookType bookType = bookTypeMapper.selectBookTypeNameByTypeUrl(typeUrl);

        if (ObjectUtils.isEmpty(bookType)) {
            throw new CustomException("无效的类别查询路径！请检查后重试");
        }

        return bookType;
    }
}
